<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * 订单控制器
 */
class OrderController extends AdminController{
    /**
     * 订单列表
     */
        public function index()
    {
       $order=D('order');
        $result=$order->index();
        //获取支付方式
        $paytype=M('pay_ment')->where('isdisplay=0')->getField('id,name');
        //订单类型
        $areatype=M('config')->where('id=4')->getField('value');
        $areatype=unserialize($areatype);
        //获取订单状态(0未确认，2取消，1确认，3已付款，6配货中，8已发货，9已收货，4退货)
        $allstatus=M('config')->where('id=5')->getField('value');
        $allstatus=unserialize($allstatus);
        //获取此运营商下所有商家
        $yid=!empty($_SESSION['yid'])?$_SESSION['yid']:0;
        if(!empty($yid))
        {
             $allshop=M('shop_user')->where('founder=%d',$yid)->getField('shopid,companyname');
        }else{
            $allshop='';
        }
        $pagearr=$result['pagearr'];
        $orders=$result['orders'];
        $this->assign('allstatus',$allstatus);
        $this->assign('allshop',$allshop);
        $this->assign('areatype',$areatype);
        $this->assign('paytype',$paytype);
        $this->assign('pagearr',$pagearr);
        $this->assign('orders',$orders);
        $this->display('index');
       

    }


    /*
     * 通过ajax 订单搜索
     * @param 
     * return json
     */
    public function searchOrder()
    {
        $order=D('order');
        $result=$order->index();
        if(!empty($result['orders'])){
            $result['status']=1;
            
            echo json_encode($result,JSON_UNESCAPED_UNICODE);
            return '';

        }

        $result['status']=0;
        $result['message']='暂无数据';
        echo  json_encode($result,JSON_UNESCAPED_UNICODE);
    }



    /**
     * 订单详情
     */
    public function detail(){
        $where['a.orderid']=$_GET['id'];
        $re=M('order a')
        ->join('left join '.C(DB_PREFIX).'order_item b'.' on a.orderid=b.orderid')
        ->where($where)
        ->getField("a.orderid,a.order_sn,a.shopid,a.mobile,
            a.address,a.province,a.city,a.district,a.consignee,
            b.itemid,b.number,b.price,b.itemname,b.specvale,b.supperid,
            a.order_amount");
      
       foreach ($re as $key => $value) {
        ////供应商信息
          $re[$key]['supper']=M('supper_user')->where("supperid='%d'",$value['supperid'])->getField("supperid,mobile,contactname,telephone,address,companyname");

         
          }
        
      
   


  
      $map['itemid']=M('order_item')->where("orderid='%d'",$_GET['orderid'])->getField("itemid");
        $over=M('item')->where($map)->Field("price,supperprice")->find();
        $profit=$over['price']-$over['supperprice'];///毛利额
        $rate=$profit/$over['supperprice']*0.1*100;///毛利率
        $this->assign('re',$re)->assign('rate',$rate)->assign('profit',$profit);
        $this->display();

    }
    /*
     * 导出订单
     * @param  orderids 
     * return csv
     */
    public function putCsv(){
        $string=I('get.orderids','','strip_tags');
        $string=trim($string);
        $orderids=explode(',', $string);
        $where['a.orderid']= !empty($orderids)?array('in',$orderids):'';
        //获取订单基本信息
        $order=M('order a');
        $product=M('product');
        $item=M('item');
        $temp=$order->join('inner join '.C(DB_PREFIX).'order_item b on a.orderid=b.orderid')
                    ->where($where)
                    ->getField('a.orderid,a.order_sn,FROM_UNIXTIME(a.create_time, "%Y-%m-%d %H:%i:%S") as create_time,a.pay_type,b.itemname,b.brand,b.sn,b.specvale,b.number,a.order_amount,b.supperprice,b.price,a.shopid,a.consignee,a.mobile,a.province,a.city,a.district,a.address,b.itemid,b.productid,b.supperid');
        //获取商品库存
        foreach ($temp as $key =>&$value) {
            
            $value['alladdress']=$value['province'].$value['city'].$value['district'].$value['address'];

            if(!empty($value['productid'])){
                $value['inventory']=$product->where('productid=%d',$value['productid'])->getField('inventory');
            }else{
                $value['inventory']=$item->where('itemid=%d',$value['itemid'])->getField('inventory');
            }
            //计算毛利
            $value['grossprofit']=$value['price']-$value['supperprice'];
            $value['grossprofit']=round($value['grossprofit'],2);
            //计算毛利率
            $res=$value['grossprofit']/$value['price'];
            $value['grossmargin']=(round($res*100,2)).'%';
            //获取商家及运营商名称
            $value['shopname']=M('shop_user')->where('shopid=%d',$value['shopid'])->getField('companyname');
            $tp=M()->query('select companyname from '.C(DB_PREFIX).'yunying_user where yid=( select founder from '.C('DB_PREFIX').'shop_user where shopid= '.$value['shopid'].')');
            $value['yunname']=$tp[0]['companyname'];
            $value['pay_type']=!empty($value['pay_type'])?'支付宝支付':'银行转账';
            unset($value['shopid']);
            unset($value['itemid']);
            unset($value['productid']);
            unset($value['supperid']);
            unset($value['province']);
            unset($value['city']);
            unset($value['district']);
            unset($value['address']);
        }
        $csv=A('Admin/Csv');
        $csv_title=array('序号','订单号','下单时间','支付方式','商品名称','商品品牌','商品型号','颜色','销售数量','销售金额','商品供价','商品售价','收货人','联系方式','收货地址','库存','毛利','毛利率','销售门店','所属运营商');
        $csv->put_csv($temp,$csv_title);
    }

}